package com.ruoyi.ybUpload.controller;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.infection.domain.PatientInfo;
import com.ruoyi.infection.service.InfectionUploadService;
import com.ruoyi.ybUpload.domain.QueryParams;
import com.ruoyi.ybUpload.service.upload.YBUploadService;
import com.ruoyi.ybUpload.utils.CommonUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: 医保接口调用示例
 *
 * @author wangjl
 */
@RestController
@RequestMapping("/ybUpload/interface")
public class YBUploadController extends BaseController {

    @Autowired
    private YBUploadService ybUploadService;
    @Autowired
    private InfectionUploadService infectionUploadService;


    private static final String url = "http://10.98.34.102/mbs-hiIntf-hechi/web/api/fsi/callService";
    private static final String infectionURL = "http://172.16.25.173:8881/hclient/emr/receive/emrPatientInfo";
    /** 按照报文要求传入JSON格式字符串 */
    private static final String downInput = "{…}";

    @GetMapping("/test")
    public AjaxResult test(QueryParams queryParams)
    {
        String mainForm = ybUploadService.uploadData(queryParams);
//        List<PatientInfo> list = infectionUploadService.selectPatientInfo();
//        for (PatientInfo p:list) {
//            System.out.println(p.toString());
//            String connect = connect(JSON.toJSONString(p));
//            System.out.println("result："+connect);
//        }
        return success("mainForm");
    }

    public String connect(String json) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(infectionURL);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
        httppost.setConfig(requestConfig);
        ByteArrayEntity entity = new ByteArrayEntity(json.getBytes(StandardCharsets.UTF_8));
        entity.setContentType("text/plain");
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                httppost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity responseEntity = response.getEntity();
            String result;
            if (responseEntity != null) {
                //返回字符串
                result = EntityUtils.toString(responseEntity, "UTF-8");
                return  result;
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            throw new RuntimeException("提交给服务器的请求，不符合HTTP协议", e);
        } catch (IOException e) {
            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 调用普通交易及文件下载交易
     */
    @Test
    public void test1() {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
        httppost.setConfig(requestConfig);
        ByteArrayEntity entity = new ByteArrayEntity(interface3505.getBytes(StandardCharsets.UTF_8));
        entity.setContentType("text/plain");
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                httppost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity responseEntity = response.getEntity();
            String result;
            if (responseEntity != null) {
                //返回字符串
                result = EntityUtils.toString(responseEntity, "UTF-8");
                System.out.println("传递参数："+interface3505);
                System.out.println("返回结果："+result);
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            throw new RuntimeException("提交给服务器的请求，不符合HTTP协议", e);
        } catch (IOException e) {
            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static final String interface3505 = "{\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"inf_time\": \"2024-08-28 10:13:33\",\n" +
            "    \"infno\": \"3505\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"input\": {\n" +
            "        \"selinfo\": {\n" +
            "            \"drugtracinfo\": [\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"(01)06939385201211(11)221219(17)271218(10)30232215\"\n" +
            "                }\n" +
            "            ],\n" +
            "            \"med_list_codg\": \"C02064206501001100550000001\",\n" +
            "            \"fixmedins_hilist_id\": \"H45120200008\",\n" +
            "            \"fixmedins_hilist_name\": \"河池市人民医院\",\n" +
            "            \"fixmedins_bchno\": \"YX24030061\",\n" +
            "            \"prsc_dr_name\": \"韦凯文\",\n" +
            "            \"phar_name\": \"韦凯文\",\n" +
            "            \"phar_prac_cert_no\": \"000000000000015\",\n" +
            "            \"mdtrt_sn\": \"000000000000015\",\n" +
            "            \"manu_lotnum\": \"231214KK\",\n" +
            "            \"manu_date\": \"2024-05-05\",\n" +
            "            \"rx_flag\": \"1\",\n" +
            "            \"trdn_flag\": \"0\",\n" +
            "            \"rtal_docno\": \"000000000000015\",\n" +
            "            \"sel_retn_cnt\": \"20\",\n" +
            "            \"sel_retn_time\": \"2024-08-28\",\n" +
            "            \"sel_retn_opter_name\": \"韦凯文\",\n" +
            "            \"mdtrt_setl_type\": \"2\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"msgid\": \"H45120200008202408281013330001\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"sign_no\": \"451200G0000081084420\"\n" +
            "}";


    private static final String interface3502 = "{\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"inf_time\": \"2024-08-09 16:13:33\",\n" +
            "    \"infno\": \"3502\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"input\": {\n" +
            "        \"invinfo\": {\n" +
            "            \"cnt\": 1,\n" +
            "            \"drugtracinfo\": [\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"84155760017822927726\"\n" +
            "                }\n" +
            "            ],\n" +
            "            \"fixmedins_bchno\": \"PJK240625R0004\",\n" +
            "            \"fixmedins_hilist_id\": \"H45120200008\",\n" +
            "            \"fixmedins_hilist_name\": \"河池市人民医院\",\n" +
            "            \"inv_chg_time\": 1719244800000,\n" +
            "            \"inv_chg_time_string\": \"2024-6-25\",\n" +
            "            \"inv_chg_type\": \"102\",\n" +
            "            \"med_list_codg\": \"XR01ACA210A001010200151\",\n" +
            "            \"pric\": 11.06,\n" +
            "            \"rx_flag\": \"1\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"msgid\": \"H45120200008202408091613330001\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"sign_no\": \"451200G0000077764074\"\n" +
            "}";


    private static final String interface3501 = "{\n" +
            "    \"infno\": \"3501\",\n" +
            "    \"msgid\": \"H45120200008202408091039450001\",\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"insuplc_admdvs\": \"\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"dev_no\": \"\",\n" +
            "    \"dev_safe_info\": \"\",\n" +
            "    \"cainfo\": \"\",\n" +
            "    \"signtype\": \"\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"inf_time\": \"2024-08-05 15:11:40\",\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"sign_no\": \"451200G0000077764074\",\n" +
            "    \"app_id\": \"\",\n" +
            "    \"enc_type\": \"\",\n" +
            "    \"input\": {\n" +
            "        \"invinfo\": {\n" +
            "            \"med_list_codg\": \"XG04BDT121A001010102135\",\n" +
            "            \"fixmedins_hilist_id\": \"H45120200008\",\n" +
            "            \"fixmedins_hilist_name\": \"河池市人民医院\",\n" +
            "            \"rx_flag\": \"1\",\n" +
            "            \"invdate\": \"2024-08-09\",\n" +
            "            \"inv_cnt\": \"10\",\n" +
            "            \"manu_lotnum\": \"240501\",\n" +
            "            \"fixmedins_bchno\": \"PJK240801R0026\",\n" +
            "            \"manu_date\": \"2024-05-05\",\n" +
            "            \"expy_end\": \"2026-05-04\",\n" +
            "            \"memo\": \"\",\n" +
            "            \"drugtracinfo\": [\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021077794658\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021043144715\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021042996798\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021042848525\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021042690551\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021042547355\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021097595775\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021097444314\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021097295212\"\n" +
            "                },\n" +
            "                {\n" +
            "                    \"drug_trac_codg\": \"83830230021097141051\"\n" +
            "                }\n" +
            "            ]\n" +
            "        }\n" +

            "    },\n" +
            "    \"pw_ecToken\": \"\"\n" +
            "}";

    private static final String interface3508 = "{\n" +
            "    \"infno\": \"3508\",\n" +
            "    \"msgid\": \"H45120200008202408051455400005\",\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"insuplc_admdvs\": \"\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"dev_no\": \"\",\n" +
            "    \"dev_safe_info\": \"\",\n" +
            "    \"cainfo\": \"\",\n" +
            "    \"signtype\": \"\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"inf_time\": \"2024-08-05 14:55:40\",\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"sign_no\": \"451200G0000077075025\",\n" +
            "    \"app_id\": \"\",\n" +
            "    \"enc_type\": \"\",\n" +
            "    \"input\": {\n" +
            "        \"data\": {\n" +
            "            \"fixmedins_code\": \"H45120200008\",\n" +
//            "            \"medins_list_codg\": \"XG04BDT121A001010102135\",\n" +
            "            \"fixmedins_bchno\": \"PJK240801R0026\",\n" +
            "            \"begndate\": \"2022-03-03\",\n" +
            "            \"enddate\": \"2028-12-05\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"pw_ecToken\": \"\"\n" +
            "}";

    private static final String interface3509 = "{\n" +
            "    \"infno\": \"3509\",\n" +
            "    \"msgid\": \"H45120200008202408051455400006\",\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"insuplc_admdvs\": \"\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"dev_no\": \"\",\n" +
            "    \"dev_safe_info\": \"\",\n" +
            "    \"cainfo\": \"\",\n" +
            "    \"signtype\": \"\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"inf_time\": \"2024-08-05 14:55:40\",\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"sign_no\": \"451200G0000077075025\",\n" +
            "    \"app_id\": \"\",\n" +
            "    \"enc_type\": \"\",\n" +
            "    \"input\": {\n" +
            "        \"data\": {\n" +
            "            \"fixmedins_code\": \"H45120200008\",\n" +
//            "            \"medins_list_codg\": \"XG04BDT121A001010102135\",\n" +
            "            \"fixmedins_bchno\": \"PJK240801R0006\",\n" +
            "            \"begndate\": \"2022-03-03\",\n" +
            "            \"enddate\": \"2028-12-05\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"pw_ecToken\": \"\"\n" +
            "}";

    private static final String signIn ="{\n" +
            "    \"recer_admvs\": \"451299\",\n" +
            "    \"opter\": \"3654\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"msgid\": \"H45120200008202408130751380001\",\n" +
            "    \"inf_time\": \"2024-08-13 07:51:38\",\n" +
            "    \"input\": {\n" +
            "        \"signIn\": {\n" +
            "            \"mac\": \"E0-BE-03-56-AD-A3\",\n" +
            "            \"opter_no\": \"3654\",\n" +
            "            \"ip\": \"192.168.100.22\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"infno\": \"9001\",\n" +
            "    \"cainfo\": \"\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"opter_name\": \"韦凯文\",\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"signtype\": \"\",\n" +
            "    \"recer_admdvs\": \"451299\",\n" +
            "    \"dev_safe_info\": \"\",\n" +
            "    \"insuplc_admdvs\": \"451202\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"sign_no\": \"\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"dev_no\": \"\"\n" +
            "}";

    private static final String hospitalInfo ="{" +
            "    \"infno\": \"3501\"," +
            "    \"msgid\": \"H45120200008202408031455330003\"," +
            "    \"mdtrtarea_admvs\": \"451229\"," +
            "    \"insuplc_admdvs\": \"\"," +
            "    \"recer_sys_code\": \"MBS_LOCAL\"," +
            "    \"dev_no\": \"\"," +
            "    \"dev_safe_info\": \"\"," +
            "    \"cainfo\": \"\"," +
            "    \"signtype\": \"\"," +
            "    \"infver\": \"V1.0\"," +
            "    \"opter_type\": \"1\"," +
            "    \"opter\": \"01\"," +
            "    \"opter_name\": \"韦凯文\"," +
            "    \"inf_time\": \"2024-08-03 17:39:15\"," +
            "    \"fixmedins_code\": \"H45120200008\"," +
            "    \"fixmedins_name\": \"河池市人民医院\"," +
            "    \"sign_no\": \"\"," +
            "    \"app_id\": \"\"," +
            "    \"enc_type\": \"\"," +
            "    \"input\": {" +
            "    \t\"data\": {" +
                    "{" +
                    "    \"fixmedins_type\": \"1\"," +
                    "    \"fixmedins_name\": \"河池市人民医院\"," +
                    "    \"fixmedins_code\": \"H45120200008\"" +
                    "}" +
            "    }," +
            "    \"pw_ecToken\": \"\"" +
            "}";


    private static final String personInfo ="{\n" +
            "    \"recer_admvs\": \"451299\",\n" +
            "    \"opter\": \"0068\",\n" +
            "    \"recer_sys_code\": \"451299\",\n" +
            "    \"msgid\": \"H45120200008202407300852330002\",\n" +
            "    \"inf_time\": \"2024-07-30 08:52:33\",\n" +
            "    \"input\": {\n" +
            "        \"data\": {\n" +
            "            \"certno\": \"452731199003230311\",\n" +
            "            \"psn_cert_type\": \"\",\n" +
            "            \"psn_name\": \"\",\n" +
            "            \"mdtrt_cert_no\": \"452731199003230311\",\n" +
            "            \"mdtrt_cert_type\": \"02\",\n" +
            "            \"card_sn\": \"\",\n" +
            "            \"begntime\": \"2024-07-30 08:52:33\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"infno\": \"1101\",\n" +
            "    \"cainfo\": \"AA38ED5875FF3E3CE384041779C04B201BAF249A659AC55643D4240A52FF17F5\",\n" +
            "    \"opter_type\": \"1\",\n" +
            "    \"opter_name\": \"葛晓英\",\n" +
            "    \"mdtrtarea_admvs\": \"451299\",\n" +
            "    \"signtype\": \"SM3\",\n" +
            "    \"recer_admdvs\": \"451299\",\n" +
            "    \"dev_safe_info\": \"\",\n" +
            "    \"insuplc_admdvs\": \"451202\",\n" +
            "    \"infver\": \"V1.0\",\n" +
            "    \"sign_no\": \"451200G0000075718682\",\n" +
            "    \"fixmedins_name\": \"河池市人民医院\",\n" +
            "    \"fixmedins_code\": \"H45120200008\",\n" +
            "    \"dev_no\": \"\"\n" +
            "}";

    /**
     * 调用文件上传交易
     */

    public void test2() {
        //File file = new File("testUpload.txt");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
        httppost.setConfig(requestConfig);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(StandardCharsets.UTF_8);
        builder.addTextBody("jsonStr", personInfo);
        //builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, "testUpload.txt");
        HttpEntity entity = builder.build();
        httppost.setEntity(entity);
        CloseableHttpResponse response = null;
        try {
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                httppost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity responseEntity = response.getEntity();
            String result;
            if (responseEntity != null) {
                //返回字符串
                result = EntityUtils.toString(responseEntity, "UTF-8");
                System.out.println("传递参数："+personInfo);
                System.out.println("返回结果："+result);
            }
            EntityUtils.consume(entity);
        } catch (ClientProtocolException e) {
            throw new RuntimeException("提交给服务器的请求，不符合HTTP协议", e);
        } catch (IOException e) {
            throw new RuntimeException("向服务器承保接口发起http请求,执行post请求异常", e);
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpclient != null) {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}